<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="${sourceBasePackage}.mapper.${entityName}Mapper">

	<parameterMap id="parameterMap" type="${sourceBasePackage}.dto.${entityName}SearchDto"></parameterMap>
	<resultMap id="resultMap" type="${sourceBasePackage}.dto.${entityName}Dto"></resultMap>

	<select id="getList" parameterMap="parameterMap" resultMap="resultMap">
		select
			<#list fields as po>
			t.${po.fieldName}<#if po_has_next>,</#if>
			</#list>
		from ${tableName} t
		<where>
			<#list searchFields as po>
				<#if po.listQuery == 'interval'>
					<#if po.propertyType == 'String'>
			<if test="${po.propertyName}Start != null and ${po.propertyName}Start != ''">
				and t.${po.fieldName} >= ${'#'}{${po.propertyName}Start}
			</if>
			<if test="${po.propertyName}End != null and ${po.propertyName}End != ''">
				and t.${po.fieldName} <![CDATA[<=]]> ${'#'}{${po.propertyName}End}
			</if>
					<#else>
			<if test="${po.propertyName}Start != null">
				and t.${po.fieldName} >= ${'#'}{${po.propertyName}Start}
			</if>
			<if test="${po.propertyName}End != null">
				and t.${po.fieldName} <![CDATA[<=]]> ${'#'}{${po.propertyName}End}
			</if>
					</#if>
				<#elseif po.listQuery == 'between'>
					<#if po.propertyType == 'String'>
			<if test="${po.propertyName}Start != null and ${po.propertyName}Start != '' and ${po.propertyName}End != null and ${po.propertyName}End != ''">
					<#else>
			<if test="${po.propertyName}Start != null and ${po.propertyName}End != null">
					</#if>
				and t.${po.fieldName} between ${'#'}{${po.propertyName}Start} and ${'#'}{${po.propertyName}End}
			</if>
				<#else>
					<#if po.propertyType == 'String'>
			<if test="${po.propertyName} != null and ${po.propertyName} != ''">
					<#else>
			<if test="${po.propertyName} != null">
					</#if>
					<#if po.listQuery == 'like'>
				and t.${po.fieldName} like concat('%',${'#'}{${po.propertyName}},'%')
					<#elseif po.listQuery == '<=' || po.listQuery == '<'>
				and t.${po.fieldName} <![CDATA[${po.listQuery}]]> ${'#'}{${po.propertyName}}
					<#else>
				and t.${po.fieldName} ${po.listQuery} ${'#'}{${po.propertyName}}
					</#if>
			</if>
				</#if>
			</#list>
		</where>
		<#if sortFields?? && (sortFields?size > 0)>
		order by<#list sortFields as po> t.${po.fieldName} ${po.listSort}<#if po_has_next>,</#if></#list>
		</#if>
	</select>

	<select id="getById" parameterType="java.lang.${pkPropertyType}" resultMap="resultMap">
		select
			<#list fields as po>
			t.${po.fieldName}<#if po_has_next>,</#if>
			</#list>
		from ${tableName} t
		where t.${pkFieldName} = ${'#'}{${pkPropertyName}}
	</select>

	<insert id="add" parameterMap="parameterMap">
		insert into ${tableName}
		<trim prefix="(" suffix=")" suffixOverrides=",">
			<#list fields as po>
			<if test="${po.propertyName} != null">
				${po.fieldName},
			</if>
			</#list>
		</trim>
		<trim prefix="values (" suffix=")" suffixOverrides=",">
			<#list fields as po>
			<if test="${po.propertyName} != null">
				${'#'}{${po.propertyName}},
			</if>
			</#list>
		</trim>
	</insert>

	<update id="update" parameterMap="parameterMap">
		update ${tableName}
		<set>
			<#list fields as po>
				<#if po.primaryKey == 'N'>
			<if test="${po.propertyName} != null">
				${po.fieldName} = ${'#'}{${po.propertyName}},
			</if>
				</#if>
			</#list>
		</set>
		where ${pkFieldName} = ${'#'}{${pkPropertyName}}
	</update>

	<delete id="delete" parameterType="java.lang.${pkPropertyType}">
		delete from ${tableName} where ${pkFieldName} = ${'#'}{value}
	</delete>

</mapper>
